package com.hongzhimei.yukicomic.activity;

import android.content.Intent;
import android.os.Bundle;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hongzhimei.yukicomic.R;
import com.hongzhimei.yukicomic.fragment.CommunityFrament;
import com.hongzhimei.yukicomic.fragment.HomeFrament;
import com.hongzhimei.yukicomic.fragment.MineFrament;
import com.hongzhimei.yukicomic.fragment.WelfareFrament;
import com.hongzhimei.yukicomic.utils.AppConstant;
import com.hongzhimei.yukicomic.utils.SPUtils;


import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Nick CY on 2016/12/20 14:46
 * 邮箱：fsi000044@163.com
 */

public class MainAcitivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.fragment)
    FrameLayout fragment;
    @Bind(R.id.rb_homepage)
    RadioButton rbHomepage;
    @Bind(R.id.rb_community)
    RadioButton rbCommunity;
    @Bind(R.id.rb_welfare)
    RadioButton rbWelfare;
    @Bind(R.id.rb_mine)
    RadioButton rbMine;
    @Bind(R.id.rgcontroller)
    RadioGroup rgcontroller;

    private CommunityFrament communityFrament;
    private HomeFrament homeFrament;
    private WelfareFrament welfareFrament;
    private MineFrament mineFrament;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        fragmentManager = getSupportFragmentManager();
        /**
         * 初始化监听事件
         */
        initlistener();
    }

    private void initlistener() {
        rgcontroller.setOnCheckedChangeListener(this);
        rgcontroller.check(R.id.rb_homepage);  //默认选中第一页
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rb_homepage:
                showFragment(1);
                break;
            case R.id.rb_community:
                showFragment(2);
                break;
            case R.id.rb_welfare:
                showFragment(3);
                break;
            case R.id.rb_mine:
                    showFragment(4);
                break;
        }

    }


    /**
     * 显示fragment
     */
    private void showFragment(int index){

        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);/*想要显示一个fragment,先隐藏所有fragment，防止重叠*/
        switch (index){
            case 1:
                /*如果fragment1已经存在则将其显示出来*/
                if (homeFrament!= null)
                    fragmentTransaction.show(homeFrament);
			    /*否则是第一次切换则添加fragment1，注意添加后是会显示出来的，replace方法也是先remove后add*/
                else {
                    homeFrament = new HomeFrament();
                    fragmentTransaction.add(R.id.fragment, homeFrament);
                }
                break;
            case 2:
                if(communityFrament!=null)
                    fragmentTransaction.show(communityFrament);
                else{

                    communityFrament = new CommunityFrament();
                    fragmentTransaction.add(R.id.fragment, communityFrament);
                }
                break;
            case 3:
                if( welfareFrament!=null)
                    this.fragmentTransaction.show(welfareFrament);
                else{

                    welfareFrament = new WelfareFrament();
                    this.fragmentTransaction.add(R.id.fragment,  welfareFrament);
                }
                break;
            case 4:
                if(mineFrament !=null)
                    this.fragmentTransaction.show(mineFrament );
                else{
                    mineFrament = new MineFrament();
                    this.fragmentTransaction.add(R.id.fragment, mineFrament );
                }
                break;

        }
        this.fragmentTransaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏fragment
     */
    private void hideFragment(FragmentTransaction ft){
        if(homeFrament!=null)
            ft.hide(homeFrament);
        if(communityFrament!=null)
            ft.hide(communityFrament);
        if(welfareFrament!=null)
            ft.hide(welfareFrament);
        if(mineFrament!=null)
            ft.hide(mineFrament);

    }
}
